/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */

package com.hopes.bizz.risk.setting.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopes.bizz.risk.setting.entity.RiskConfigLEntity;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.log.annotation.SysLog;
import com.hopes.bizz.risk.setting.entity.RiskConfigLsEntity;
import com.hopes.bizz.risk.setting.dto.RiskConfigLsExcelDTO;
import com.hopes.bizz.risk.setting.vo.RiskConfigLsExcelVO;
import com.hopes.bizz.risk.setting.service.RiskConfigLsService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.hopes.boss.common.excel.annotation.RequestExcel;
import com.hopes.boss.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * LS评级结果设置
 *
 * @author pig
 * @date 2023-07-17 19:43:01
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/riskConfigLs" )
@Tag(description = "riskConfigLs" , name = "LS评级结果设置管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class RiskConfigLsController {

    private final RiskConfigLsService riskConfigLsService;


    /**
     * LS初始化表单数据
     */
    @Operation(summary = "LS初始化表单数据" , description = "LS初始化表单数据" )
    @GetMapping("/initIdentifyData" )
    public R initIdentifyData() {
        return R.ok(riskConfigLsService.initIdentifyData());
    }

    /**
     * 初始数据
     * @return
     */
    @Operation(summary = "初始数据" , description = "初始数据" )
    @GetMapping("/init" )
    @PreAuthorize("@pms.hasPermission('risk_evaluate_method_view')" )
    public R getRiskConfigLsInitMap(RiskConfigLsEntity riskConfigLs) {
        return R.ok(riskConfigLsService.initMap(riskConfigLs));
    }

    /**
     * 分页查询
     * @param page 分页对象
     * @param riskConfigLs LS评级结果设置
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('risk_evaluate_method_view')" )
    public R getRiskConfigLsPage(Page page, RiskConfigLsEntity riskConfigLs) {
        LambdaQueryWrapper<RiskConfigLsEntity> wrapper = Wrappers.lambdaQuery();
        return R.ok(riskConfigLsService.page(page, wrapper));
    }




    /**
     * 通过id查询LS评级结果设置
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('risk_evaluate_method_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(riskConfigLsService.getById(id));
    }

    /**
     * 新增LS评级结果设置
     * @param riskConfigLs LS评级结果设置
     * @return R
     */
    @Operation(summary = "新增LS评级结果设置" , description = "新增LS评级结果设置" )
    @SysLog("新增LS评级结果设置" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('risk_evaluate_method_add')" )
    public R save(@RequestBody RiskConfigLsEntity riskConfigLs) {
        return R.ok(riskConfigLsService.save(riskConfigLs));
    }

    /**
     * 修改LS评级结果设置
     * @param riskConfigLs LS评级结果设置
     * @return R
     */
    @Operation(summary = "修改LS评级结果设置" , description = "修改LS评级结果设置" )
    @SysLog("修改LS评级结果设置" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('risk_evaluate_method_edit')" )
    public R updateById(@RequestBody RiskConfigLsEntity riskConfigLs) {
        return R.ok(riskConfigLsService.updateById(riskConfigLs));
    }

    /**
     * 通过id删除LS评级结果设置
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除LS评级结果设置" , description = "通过id删除LS评级结果设置" )
    @SysLog("通过id删除LS评级结果设置" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('risk_evaluate_method_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(riskConfigLsService.removeBatchByIds(CollUtil.toList(ids)));
    }

    /**
     * excel模板
     *
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/template")
    @PreAuthorize("@pms.hasPermission('risk_evaluate_method_export')")
    public List<RiskConfigLsExcelVO> template() {
        return riskConfigLsService.exportExcel(Collections.singletonList(new RiskConfigLsEntity()));
    }

    /**
     * 导出excel 表格
     * @param riskConfigLsExcelDto 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('risk_evaluate_method_export')")
    public List<RiskConfigLsExcelVO> export(RiskConfigLsExcelDTO riskConfigLsExcelDto) {
        return riskConfigLsService.exportExcel(riskConfigLsService.list());
    }

    /**
     * 导入excel 表格
     * @param riskConfigLsExcelDtoList 查询条件
     * @param bindingResult 错误信息列表
     * @return ok fail
     */
    @PostMapping("/import")
    @PreAuthorize("@pms.hasPermission('risk_evaluate_method_export')")
    public R importExcel(@RequestExcel List<RiskConfigLsExcelDTO> riskConfigLsExcelDtoList, BindingResult bindingResult) {
        return riskConfigLsService.importExcel(riskConfigLsExcelDtoList, bindingResult);
    }
}
